<?php

declare(strict_types=1);

namespace owns\library;

/**
 * 客户端
 */
class OwnsClient
{
    private $curl;

    private $decode = true; //解码

    /**
     * 初始化curl
     */
    private function init($url, $header)
    {
        if (!$header) {
            $header = [
                'Content-type:application/json;charset=UTF-8',
                'Accept:application/json'
            ];
        }
        $this->curl = curl_init();
        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
        // 打开ssl安全校验
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    /**
     * @param array $data
     */
    private static function encode(array $data, bool $url_code)
    {
        if ($url_code) {
            return http_build_query($data);
        }
        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }
    /**
     * 输出
     */
    private function output()
    {
        $output = curl_exec($this->curl);
        curl_close($this->curl);
        if ($this->decode) {
            return json_decode($output ?? '', true);
        }
        return $output;
    }

    /**
     * post
     * @param string $url 请求路径
     * @param array $param 请求参数
     * @param array $header 请求头信息
     * @param boolean $url_code http请求参数是否通过http URL编码传送
     */
    public function post(string $url, array $param, array $header = [], $url_code = false)
    {
        $this->init($url, $header);

        curl_setopt($this->curl, CURLOPT_POST, 1);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, self::encode($param, $url_code));

        return $this->output();
    }
    /**
     * get
     */
    public function get(string $url, array $param = [], array $header = [], $url_code = false)
    {
        if ($param && !$url_code) {
            $url .= '?' . http_build_query($param);
        }
        $this->init($url, $header);
        if ($param && $url_code) {
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($param));
        }

        return $this->output();
    }
    /**
     * put
     */
    public function put(string $url, array $param = [], array $header = [], $url_code = false)
    {
        $this->init($url, $header);

        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, self::encode($param, $url_code));

        return $this->output();
    }
    /**
     * delete
     */
    public function delete(string $url, array $param = [], array $header = [], $url_code = false)
    {
        $this->init($url, $header);

        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, self::encode($param, $url_code));

        return $this->output();
    }
    /**
     * patch
     */
    public function patch(string $url, array $param = [], array $header = [], $url_code = false)
    {
        $this->init($url, $header);

        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, self::encode($param, $url_code));

        return $this->output();
    }
}
